﻿using Assets.Content.Scripts.DateBase;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Content.Scripts.UI.Start
{
    public class StartManager : MonoBehaviour
    {
        private static StartManager _admin;
        public static StartManager Instance
        {
            get { return _admin; }
        }

        [SerializeField, LabelText("主界面菜单")]
        private GameObject _mainMenu;
        
        [SerializeField, LabelText("选人调整属性界面")]
        private GameObject _choosePanel;

        [SerializeField, LabelText("载入游戏界面")]
        private GameObject _loadGamePanel;

        [SerializeField, LabelText("提示信息")]
        private GameObject _messageBoxPanel;

        private     Button            _newGameButton;        // 开始新游戏按钮
        private     Button            _loadGameButton;       // 载入游戏按钮
        private     Button            _messageBoxButton;     // 提示信息确认按钮

        private void Awake()
        {
            _admin = this;

            if (AccessPlayerDate.GetFileFolder() != null && AccessPlayerDate.GetFileFolder().Length > 0)  
                _mainMenu.transform.GetChild(1).gameObject.SetActive(true);// 显示载入按钮

            _newGameButton    = _mainMenu.transform.GetChild(0).GetComponent<Button>();
            _newGameButton.onClick.AddListener(OnClickNewGameButton);
            _loadGameButton   = _mainMenu.transform.GetChild(1).GetComponent<Button>();
            _loadGameButton.onClick.AddListener(OnClickLoadGameButton);
            _messageBoxButton = _messageBoxPanel.transform.GetChild(1).GetComponent<Button>();
            _messageBoxButton.onClick.AddListener(() => { _messageBoxPanel.SetActive(false); });
        }

        #region 点击事件

        private void OnClickNewGameButton()
        {
            if (AccessPlayerDate.GetFileFolder().Length >= 5)
            {
                _messageBoxPanel.SetActive(true);
                return;
            }
            _mainMenu.SetActive(false);
            _choosePanel.SetActive(true);
        }

        private void OnClickLoadGameButton()
        {
            _mainMenu.SetActive(false);
            _loadGamePanel.SetActive(true);
            var folder = AccessPlayerDate.GetFileFolder();
            for (var i = 0; i < folder.Length; i++)
            {
                _loadGamePanel.transform.GetChild(1).GetChild(i).gameObject.SetActive(true);
                _loadGamePanel.transform.GetChild(1).GetChild(i).GetChild(0).GetChild(0).GetComponent<Text>().text = folder[i].Name.Split('-')[1];
                _loadGamePanel.transform.GetChild(1).GetChild(i).GetChild(1).GetChild(0).GetComponent<Text>().text = folder[i].GetFileSystemInfos()[0].LastWriteTime.ToLongTimeString();
            }
        }

        #endregion

        #region 外部接口

        public void DisPlay(bool value)
        {
            _mainMenu.SetActive(value);
        }

        #endregion
    }
}